package com.xs.bookspoststation.UserActivity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.db.DBHelperUser;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

    //定义对象
    private EditText edit_username1,edit_userpwd1,edit_userphone1;
    private RadioGroup user_radioGroup1;
    private RadioButton user_radioButton1,user_radioButton2;
    private CheckBox checkBox1,checkBox2,checkBox3;
    private Spinner spinner_education;
    private Button btn_cancel1,btn_register1;
    //定义一个数据库帮助类
    private DBHelperUser dbHelperUser;
    //定义一个数据库操作类
    private SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //操作对象的方法
        initView();
    }

    //操作对象
    private void initView() {
        //获取用户名控件
        edit_username1 = findViewById(R.id.edit_username1);
        //获取密码控件
        edit_userpwd1 = findViewById(R.id.edit_userpwd1);
        //获取电话控件
        edit_userphone1 = findViewById(R.id.edit_userphone1);
        //获取按钮
        user_radioGroup1 = findViewById(R.id.user_radioGroup1);
        //获取性别男控件
        user_radioButton1 = findViewById(R.id.user_radioButton1);
        //获取性别女控件
        user_radioButton2 = findViewById(R.id.user_radioButton2);
        //获取爱好控件
        checkBox1 = findViewById(R.id.checkBox1);
        //获取爱好控件
        checkBox2 = findViewById(R.id.checkBox2);
        //获取爱好控件
        checkBox3 = findViewById(R.id.checkBox3);
        //获取学历控件
        spinner_education = findViewById(R.id.spinner_education);
        //获取取消按钮控件
        btn_cancel1 = findViewById(R.id.btn_cancel1);
        //获取注册按钮控件
        btn_register1 = findViewById(R.id.btn_register1);
        //点击事件
        btn_cancel1.setOnClickListener(this);
        btn_register1.setOnClickListener(this);
        //实例化数据库帮助类对象
        dbHelperUser = new DBHelperUser(RegisterActivity.this,"books_station.db",null,1);
        //创建数据库并获取数据库的读写权限
        db = dbHelperUser.getWritableDatabase();
    }

    /**
     * 点击方法
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_cancel1:
                finish();
                break;
            case R.id.btn_register1:
                goRegister();
                break;
        }
    }

    /**
     * 注册方法
     */
    private void goRegister() {
        // 获取用户输入的数据
        String username = edit_username1.getText().toString().trim();
        String userpass = edit_userpwd1.getText().toString().trim();
        String userphone = edit_userphone1.getText().toString().trim();
        //获取性别值
        String strSex = "";
        //根据选中的id进行判断
        switch (user_radioGroup1.getCheckedRadioButtonId()){
            case R.id.user_radioButton1:
                strSex = user_radioButton1.getText().toString();
                break;
            case R.id.user_radioButton2:
                strSex = user_radioButton2.getText().toString();
                break;
        }
        //获取爱好
        StringBuilder builder = new StringBuilder();//使用字符串拼接的时候可以用StringBuilder,它的长度是可变的；可以将你复选框汇总选中的内容添加进去
        if (checkBox1.isChecked()){
            builder.append(checkBox1.getText().toString());
        }
        if (checkBox2.isChecked()){
            builder.append(checkBox2.getText().toString());
        }
        if (checkBox3.isChecked()){
            builder.append(checkBox3.getText().toString());
        }
        String strHobby = builder.toString().trim();

        boolean createUser = true;
        dbHelperUser = new DBHelperUser(RegisterActivity.this,"books_station.db",null,1);
        db = dbHelperUser.getWritableDatabase();
        if (username.equals("") || userpass.equals("") || userphone.equals("")){
            Toast.makeText(RegisterActivity.this, "您还有信息未填写！！！", Toast.LENGTH_SHORT).show();
        }else if(userpass.length() < 6 || userpass.length() > 6){
            Toast.makeText(RegisterActivity.this, "密码必须为六位数", Toast.LENGTH_SHORT).show();
        }else {
            Cursor cursor = db.query("book_user",new String[]{"username"},null,null,null,null,null);
            while (cursor.moveToNext()){
                if (username.equals(cursor.getString(cursor.getColumnIndex("username")))){
                    Toast.makeText(RegisterActivity.this, "该账号已存在", Toast.LENGTH_SHORT).show();
                    createUser = false;
                    break;
                }
            }
        }
        if (createUser){
            //存储类，只能为String类型
            ContentValues values = new ContentValues();
            values.put("username",username);
            values.put("userpwd",userpass);
            values.put("userphone",userphone);
            values.put("usersex",strSex);
            values.put("userhobby",strHobby);
            db.insert("book_user",null,values);
            db.close();//关闭数据库
            AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();
            alertDialog.setTitle("注册成功");
            alertDialog.setMessage("是否登录？");
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            alertDialog.show();
        }

    }
}
